package org.dreamwork.jasmine2.web.controls.chart.settings;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2010-5-12
 * Time: 10:23:20
 */
public class ChartSetting extends ChartSettingSection {
    private ChartSettingAttribute title = new ChartSettingAttribute ("text", "", true);
    private ChartSettingAttribute titleAlign = new ChartSettingAttribute ("align", "center");
    private ChartSettingAttribute titleFontSize = new ChartSettingAttribute ("text_size", "18");
    private ChartSettingAttribute legendEnabled = new ChartSettingAttribute ("enabled", "true");
    protected ChartSettingAttribute legendFormat = new ChartSettingAttribute ("text", "{value}/{percents}%", true);

    public static final String[] COLORS = {
            "#FF6600","#FCD202","#B0DE09","#0D8ECF","#2A0CD0","#CD0D74",
            "#CC0000","#00CC00","#0000CC","#DDDDDD","#999999","#333333","#990000"
    };

    protected Map<String, ChartSettingSection> elements = new HashMap<String, ChartSettingSection> ();

    public ChartSetting () {
        this ("xml");
    }

    public ChartSetting (String dataType) {
        addAttribute ("data_type", dataType);
        addAttribute ("redraw", "true");
        addAttribute ("decimals_separator", ".");
        addAttribute ("thousands_separator", ",");

        ChartSettingSection labels = addSection ("labels");
        ChartSettingSection label = new ChartSettingSection ("label");
        labels.addSection (label);
        label.setProperty ("lid", "0");
        label.addAttribute ("x", "0")
                .addAttribute ("y", "40")
                .addAttribute (titleAlign)
                .addAttribute (titleFontSize)
                .addAttribute (title);
        addSection ("legend")
                .addAttribute ("spacing", "5")
                .addAttribute ("margins", "3")
                .addSection ("values")
                .addAttribute (legendEnabled)
                .addAttribute (legendFormat);
    }

    public ChartSetting setTitle (String title) {
        this.title.setValue (title);
        return this;
    }

    public ChartSetting setTitleFontSize (int size) {
        titleFontSize.setValue (String.valueOf (size));
        return this;
    }

    public ChartSetting setTitleAlign (ChartAlign align) {
        titleAlign.setValue (align.toString ());
        return this;
    }

    public ChartSetting setLegendEnabled (boolean enable) {
        legendEnabled.setValue (String.valueOf (enable));
        return this;
    }

    @Override
    public String toString () {
        StringBuilder builder = new StringBuilder ();
        builder.append ("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        builder.append ("<settings>");
        for (ChartSettingAttribute attribute : attributes)
            builder.append (attribute);
        for (ChartSettingSection section : sections)
            builder.append (section);
        builder.append ("</settings>");
        return builder.toString ();
    }

    protected void setElementAttribute (String path, String name, String value) {
        getSection (path).addAttribute (name, value);
    }

    protected ChartSettingSection getSection (String path) {
        if (elements.containsKey (path)) return elements.get (path);
        int pos = path.lastIndexOf ('.');
        if (pos < 0)
            return addSection (path);

        String name = path, parentName = null;
        if (pos > -1) {
            name = path.substring (pos + 1);
            parentName = path.substring (0, pos);
        }

        ChartSettingSection p = getSection (parentName);
        ChartSettingSection s = new ChartSettingSection (name);
        p.addSection (s);
        elements.put (path, s);
        return s;
    }

    public void merge (ChartSetting setting) {
        merge (setting, false);
    }

    public void merge (ChartSetting setting, boolean replace) {

    }
}